package com.sc.web.advert;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sc.dal.model.ParamMap;
import com.sc.dal.model.advert.Advert;
import com.sc.dal.model.system.UserFormMap;
import com.sc.service.advert.AdvertService;
import com.sc.util.adapter.ConfigBeanAdapter;
import com.sc.util.data.Constants;
import com.sc.util.data.DataUtil;
import com.sc.util.data.FileUtil;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;
import com.sc.web.BaseController;
import com.sc.web.plugin.PageView;
@Controller
@RequestMapping(value="/advert")
public class AdvertController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private AdvertService advertService;
	
	@RequestMapping(value="/list")
	public String list(HttpServletRequest request,Model model){
		model.addAttribute("res", findByRes());
		return "/advert/list";
	}
	@RequestMapping(value="/selectList")
	@ResponseBody
	public PageView selectList(HttpServletRequest request,String pageNow, String pageSize, String column, String sort){
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		
		try{
			ParamMap paramMap = getFormMap(ParamMap.class);		
			paramMap = toFormMap(paramMap, pageNow, pageSize,paramMap.getStr("orderby"));
			paramMap.put("column", column);
			paramMap.put("sort", sort);
			List<Advert> advertList = this.advertService.selectList(paramMap);
			for (Advert advert : advertList) {
				advert.setImg(ConfigBeanAdapter.fileAccessAddress+advert.getImg());
			}
			pageView.setRecords(advertList);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			logger.info("查询轮播图数据成功!");
		}catch(Exception e){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.error("查询轮播图数据异常:"+e);
			throw new ScBizException("查询轮播图数据异常！", e);
		}
		return pageView;
	}
	@RequestMapping(value="/addOrEdit")
	public  String addOrEdit(Integer id,Model model){
		try {
			Advert advert=advertService.selectByPrimaryKey(id);
			if(advert==null){
				return "/advert/add";
			}
			advert.setImg(ConfigBeanAdapter.fileAccessAddress+advert.getImg());
			model.addAttribute("advert", advert);
			return "/advert/edit";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
	}
	@RequestMapping(value="/saveAdvert")
	@ResponseBody
	public String saveAdvert(HttpServletRequest request,Advert advert,MultipartFile uploadImg){
		String imgUrl="";
		String matchPath="advert/";
		try {
			UserFormMap userFormMap=(UserFormMap) SecurityUtils.getSubject().getSession().getAttribute("userSession");
			imgUrl=FileUtil.uploadByFile(uploadImg, "uploadImg", matchPath, ConfigBeanAdapter.fileSaveAddress);
			if(imgUrl!=null&&!"".equals(imgUrl)&&!"1".equals(imgUrl)&&!"2".equals(imgUrl)){				
				advert.setImg(imgUrl);
			}
			advert.setIsDelete(Constants.IS_DELETEA);
			if(advert.getaId()==null){
				advert.setCreater(userFormMap.getStr("accountName"));
				advert.setCreateTime(new Date());
				advertService.insert(advert);
			}else{
				advert.setModifier(userFormMap.getStr("accountName"));
				advert.setModifyTime(new Date());
				advertService.updateByPrimaryKeySelective(advert);
			}
			return "\"success\"";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
	}
	@ResponseBody
	@RequestMapping("deleteEntity")
	public String deleteEntity(){
		try {
			String ids = getPara("ids");
			String[] idArr=ids.split(",");
			for (String string : idArr) {
				Advert advert=new Advert();
				advert.setaId(Integer.parseInt(string));
				advert.setIsDelete(Constants.IS_DELETEB);
				advert.setModifyTime(new Date());
				advertService.updateByPrimaryKeySelective(advert);
			}
			return "\"success\"";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
	}
}
